//
//  SPAnnotationView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/14.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "SPAnnotationView.h"
#import "SPAnnotation.h"


@interface SPAnnotationView()
@property (nonatomic,weak) UIImageView *imageV;
@property (nonatomic,weak) UILabel *titleL;

@end

@implementation SPAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]){

        //初始化
        UIImageView *imageV = [[UIImageView alloc] init];
        _imageV = imageV;
        [self addSubview:imageV];
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(3);
            make.centerY.equalTo(self);
        }];
        
        UILabel *titleL = [UILabel new];
        titleL.numberOfLines = 0;
        _titleL = titleL;
        titleL.textColor = RZ_COLOR(51, 51, 51);
        titleL.font = [UIFont systemFontOfSize:12];
        [self addSubview:titleL];
        [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageV.mas_right).offset(3);
            make.width.mas_equalTo(80);
            make.centerY.equalTo(self);
        }];
        
        //给自身大小 不然点击没反应
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(titleL.mas_right);
//            make.top.bottom.left.equalTo(imageV);
            make.height.mas_equalTo(44);
        }];
        
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
//    RZLog(@"--%@",NSStringFromCGRect(self.frame));
}


+ (instancetype)annotationViewWithMapView:(MKMapView *)mapView
{
    static NSString *ID = @"annotationView";
    SPAnnotationView *annoView = (SPAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    if(!annoView){
        annoView = [[SPAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:ID];
    }
    return annoView;
}



- (void)setAnnotation:(SPAnnotation *)annotation
{
    [super setAnnotation:annotation];
    //处理自己特有的操作
    self.imageV.image = [UIImage imageNamed:annotation.icon];
    self.titleL.text = annotation.title;
}


- (void)tap
{
    !self.onClickAnnotationViewBlock?:self.onClickAnnotationViewBlock(self);
}

@end
